using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    private Rigidbody2D rBody;
    private Animator ani;
    // 判断是否碰到地面
    private bool isGround;
    public static int hp = 1;
    void Start(){
        rBody = GetComponent<Rigidbody2D>();
        ani = GetComponent<Animator>();
    }

    void Update(){
        if(Input.GetKeyDown(KeyCode.Space)){
            Jump();
        }
    }

    // 跳跃
    public void Jump(){
        if(isGround){
            rBody.AddForce(Vector2.up*400);
            AudioManger.instance.Play("跳");
        }
    }

    private void OnCollisionEnter2D(Collision2D collision) {
        if(collision.collider.tag == "Ground"){
            isGround = true;
            ani.SetBool("isJump",false);
        }
        if(collision.collider.tag == "Die" && hp>0){
            hp = 0;
            AudioManger.instance.Play("Boss死了");
            ani.SetBool("isDie",true);
        }
        if(collision.collider.tag == "Enemy" && hp>0){
            hp = 0;
            AudioManger.instance.Play("Boss死了");
            ani.SetBool("isDie",true);
        }
    }

    private void OnCollisionExit2D(Collision2D collision) {
        isGround = false;
        ani.SetBool("isJump",true);
    }
}
